﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FzWeb.Models;
using System.Web.Mvc;
using FzWeb.Filter;
using System.Text.RegularExpressions;
using System.Collections;

/// <summary>
///AreaController 的摘要说明
/// </summary>
/// 

namespace FzWeb.Controllers
{
    [ExceptionFilter]
    public class AreaController : Controller
    {
        private BaseDataService bds = new BaseDataService();
        public ActionResult Index()
        {
            if (bds.GetAreaList().Count == 0)
                TempData["maxOrder"] = 1;
            else
                TempData["maxOrder"] = bds.GetAreaList().Last().Order + 1;
            return View(bds.GetTopAreaList());
        }

        public ActionResult Details(int Id)
        {
            TempData["maxOrder"] = bds.GetAreaList().Last().Order + 1;
            var currarea = bds.GetArea(d => d.AreaId == Id);

            ViewData["ParentAreaId"] = Id;
            ViewData["ParentAreaName"] = currarea.Name;
            TempData["ParentAreaId"] = Id;

            currarea.Childs.Load();
            return View(currarea.Childs.ToList());
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Area a)
        {
            try
            {
                a.Order = (int)TempData["maxOrder"];
                bds.CreateArea(a);
                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateDetail(Area a)
        {
            try
            {
                a.Order = (int)TempData["maxOrder"];
                int id=(int)TempData["ParentAreaId"];
                a.Parent = bds.GetArea(c => c.AreaId == id);
                bds.CreateArea(a);
                return RedirectToAction("details", new { id=a.Parent.AreaId});
            }
            catch
            {
                return RedirectToAction("details", new { id = a.Parent.AreaId });
            }
        }

        public ActionResult Delete(int Id)
        {
            Area area = bds.GetArea(a => a.AreaId == Id);
            bds.DeleteArea(area);
            return RedirectToAction("Index");
        }

        public ActionResult DeleteDetail(int Id)
        {
            Area parent = bds.GetArea(a => a.AreaId == Id).Parent;
            bds.DeleteArea(bds.GetArea(a => a.AreaId == Id));
            return RedirectToAction("details", new { id = parent.AreaId });
        }

        public ActionResult Edit(int Id)
        {
            return View(bds.GetArea(a => a.AreaId == Id));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Area a)
        {
            try
            {
                bds.UpdateArea(a);
                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult EditDetail(int Id)
        {
            return View(bds.GetArea(a => a.AreaId == Id));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditDetail(Area pl)
        {
            try
            {
                //客户端上传的时候由于没有PARENT的指定 导致导航属性没有 
                //不能指定到PARENT 只好根据原ID从服务器端重新获取
                int id = pl.AreaId;
                Area pc = bds.GetArea(c => c.AreaId == id);
                bds.UpdateArea(pl);
                return RedirectToAction("details", new { id = pc.Parent.AreaId});
            }
            catch
            {
                return View();
            }
        }

        public ActionResult UpDetail(int Id)
        {
            Area a = bds.GetArea(c => c.AreaId == Id);
            Area parent = a.Parent;
            if (a.Order > 1)
            {
                Area before = bds.GetBeforeArea(a.Order, parent);
                int temp = a.Order;
                a.Order = before.Order;
                before.Order = temp;
                bds.UpdateArea(a);
                bds.UpdateArea(before);
            }
            return RedirectToAction("details", new { id = parent.AreaId });
        }

        public ActionResult DownDetail(int Id)
        {
            Area a = bds.GetArea(c => c.AreaId == Id);
            Area parent = a.Parent;

            //bds.GetChildsAreaList(a);
            if (a.Order < (int)TempData["maxOrder"] - 1)
            {
                Area after = bds.GetAfterArea(a.Order, parent);
                int temp = a.Order;
                a.Order = after.Order;
                after.Order = temp;
                bds.UpdateArea(a);
                bds.UpdateArea(after);
            }
            return RedirectToAction("details", new { id = parent.AreaId });
        }

        public ActionResult Up(int Id)
        {
            Area a = bds.GetArea(c => c.AreaId == Id);
            if (a.Order > 1)
            {
                Area before = bds.GetBeforeArea(a.Order);
                int temp = a.Order;
                a.Order = before.Order;
                before.Order = temp;
                bds.UpdateArea(a);
                bds.UpdateArea(before);
            }
            return RedirectToAction("Index");
        }

        public ActionResult Down(int Id)
        {
            Area a = bds.GetArea(c => c.AreaId == Id);
            if (a.Order < (int)TempData["maxOrder"] - 1)
            {
                Area after = bds.GetAfterArea(a.Order);
                int temp = a.Order;
                a.Order = after.Order;
                after.Order = temp;
                bds.UpdateArea(a);
                bds.UpdateArea(after);
            }
            return RedirectToAction("Index");
        }




        //[AcceptVerbs(HttpVerbs.Post)]
        //public ActionResult Delete(Area a)
        //{
        //    try
        //    {
        //        bds.DeleteArea(bds.GetArea(c => c.AreaCode == a.AreaCode));
        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return RedirectToAction("Index");
        //    }
        //}

        //#region 
        ////取键值对应的上级编码 
        ////无参数方法取全部列表 一般用于CREATE方法中要选取所有的
        ////带参数方法取全部列表 中除自身要修改的对象外所有的对象，因为修改时不能将父类别指向自己的ID，那样父ID和主键ID重复
        //private void getParentAreaIdList()
        //{
        //    var dic = new Dictionary<int, string>();
        //    var Areas = bds.GetAreaList();
        //    dic.Add(0, "中国");
        //    foreach (Area a in Areas)
        //    {
        //        dic.Add(a.AreaId, a.Name);
        //    }
        //    var sel = new ArrayList();
        //    sel.Add("2");
        //    ViewData["ParentAreaId"] = new MultiSelectList(dic, "Key", "Value", sel);
        //    //
        //}

        //private void getParentAreaCodeList(int areacode)
        //{
        //    var dic = new Dictionary<int, string>();
        //    dic.Add(0, "中国");
        //    var Areas = bds.GetAreaList();
        //    foreach (Area a in Areas)
        //    {
        //        if (a.AreaId != areacode)
        //            dic.Add(a.AreaId, a.Name);
        //    }
        //    var sel = new ArrayList();
        //    sel.Add("2");
        //    ViewData["ParentAreaId"] = new MultiSelectList(dic, "Key", "Value", sel);
        //}
        // #endregion
        //protected void Validate(Area a)
        //{

        //    if (a.AreaCode.Trim().Length == 0)
        //        ModelState.AddModelError("areacode", "地区编码必填.");
        //    if (a.AreaName.Trim().Length == 0)
        //        ModelState.AddModelError("areaname", "地区名称必填.");

        //    if (a.Order.Equals(null) || !Regex.IsMatch(a.Order.ToString(), @"^-?[1-9]\d*$)"))
        //        ModelState.AddModelError("order", "地区排序必填并且为整数.");
        //    ////后面可以依次写上

        //    //if (ValidateService.IsEmptyString(a.AreaCode))
        //    //{
        //    //    ModelState.AddModelError("areacode", "地区编码必填.");
        //    //}

        //    //if (ValidateService.IsEmptyString(a.AreaName))
        //    //{
        //    //    ModelState.AddModelError("areaname", "地区名称必填.");
        //    //}
        //    //if(!ValidateService.IsInt(a.Order.ToString()))
        //    //{
        //    //    ModelState.AddModelError("order", "地区排序必填并且为整数.");
        //    //}
        //}


      



    }
}